﻿using Sidvall.Net;
using System.Threading.Tasks;

namespace Sidvall.IdentityManager.Test
{
    public static class Program
    {
        #region Main

        public static void Main(string[] args)
        {
            try
            {
                RegisterTypes();

                // Read data from database
                var users = Sidvall.IdentityManager.SystemContext.Current.DataPortal.AspNetUsersManager.GetUsersAsync(null, false, true, true, null, null).Result;

                // Save data to files
                var folder = System.IO.Path.Combine(Properties.Settings.Default.FileStoragePath, "AspNetUsers");
                if (Sidvall.SystemContext.Current.SystemStorageManager.FolderExistsAsync(folder).Result)
                    Sidvall.SystemContext.Current.SystemStorageManager.DeleteFolderAsync(folder, true).Wait();
                Sidvall.IdentityManager.SystemContext.Current.InitializeFileStorageDataLayer(Properties.Settings.Default.FileStoragePath);
                users = users.SaveItemsAsync(Sidvall.Data.SaveMode.ForceAddForAllKeepKey).Result;

                System.Console.WriteLine("Done.");
            }
            catch (System.Exception ex)
            {
                System.Console.WriteLine(Sidvall.SystemContext.Current.SystemExceptionManager.GetMessage(ex));
            }
            System.Console.ReadLine();
        }

        #endregion

        #region FrameworkFeatures

        private static async Task FrameworkFeatures()
        {
            // File Management - requires Sidvall.IO
            var files = await Sidvall.SystemContext.Current.SystemStorageManager.GetFilesAsync(@"c:\", null, Sidvall.IO.SearchOption.TopDirectoryOnly, false).ConfigureAwait(false);
            // Serialization
            var json = Sidvall.Serialization.SerializerManager.Current.SerializeObject(files);
            // Storage Management - requires Sidvall.IO.Storage
            await Sidvall.SystemContext.Current.UserStorageManager.WriteAllTextAsync("UserSetting.txt", "my settings").ConfigureAwait(false);
            var userSetting = await Sidvall.SystemContext.Current.UserStorageManager.ReadAllTextAsync("UserSetting.txt").ConfigureAwait(false);
            // Mail Management - requires Sidvall.Net.Mail
            //Sidvall.SystemContext.Current.SmtpClientManager.Send(new string[] { "{ToAddress" }, "Subject", "Body", false);
            // REST client - requires Sidvall.Net
            var restClient = Sidvall.SystemContext.Current.HttpClientFactory.GetHttpClient(new HttpClientSettings());
            //var restResponse = await restClient.GetAsync("{url}").ConfigureAwait(false);
            // SOAP client - requires Sidvall.Net
            var soapClient = Sidvall.SystemContext.Current.SoapClientFactory.GetSoapClient(new SoapClientSettings());
            //var soapResponse = await soapClient.SendSoapAsync("{url}", "content", "action").ConfigureAwait(false);
            // Transaction
            using (var transaction = Sidvall.SystemContext.Current.TransactionScopeFactory.GetTransactionScope())
            {
                transaction.Complete();
            }
            // Global Cache - requires Sidvall.Caching
            Sidvall.SystemContext.Current.CacheManager.AddOrUpdate("key", 5, "region", new System.TimeSpan(1000));
            // Settings
            var setting = Sidvall.SystemContext.Current.SettingsManager.GetSetting("TestSetting");
            // Resources
            var resource = Sidvall.SystemContext.Current.ResourceManager.GetString("TestResource");
        }

        #endregion
        #region RegisterTypes

        private static void RegisterTypes()
        {
            // Framework
            var authorizationProvider = new Sidvall.Security.PublicAuthorizationProvider();
            Sidvall.SystemContext.Current = new Sidvall.SystemContext()
            {
                AuthorizationManager = new Sidvall.Security.AuthorizationManager(authorizationProvider),
                //AuthenticationManager = new Sidvall.Security.AuthenticationManager(new Sidvall.Security.AuthenticationProvider()),
                CacheManager = new Sidvall.Caching.CacheManager(new Sidvall.Caching.MemoryCache()),
                //ClaimManager = new Sidvall.Security.ClaimManager(new Sidvall.Security.ClaimsPrincipalManager()),
                CodeManager = new Sidvall.Reflection.CodeManager(new Sidvall.Reflection.Code()),
                UserExceptionManager = new Sidvall.ExceptionHandling.ExceptionManager(new Sidvall.ExceptionHandling.UserExceptionProvider()),
                SystemExceptionManager = new Sidvall.ExceptionHandling.ExceptionManager(new Sidvall.ExceptionHandling.SystemExceptionProvider()),
                HttpClientFactory = new Sidvall.Net.HttpClientFactory(),
                //LogManager = new Sidvall.Logging.LogManager(new Sidvall.Logging.EL.Log()),
                SmtpClientManager = new Sidvall.Net.Mail.SmtpClientManager(new Sidvall.Net.Mail.SmtpClient()),
                SoapClientFactory = new Sidvall.Net.SoapClientFactory(),
                SystemStorageManager = new Sidvall.IO.StorageManager(new Sidvall.IO.FileSystem()),
                //TransactionScopeFactory = new Sidvall.Data.TransactionScopeFactory(),
                TypeCache = new Sidvall.Reflection.TypeCache(),
                UserManager = new Sidvall.Security.UserManager(authorizationProvider),
                UserStorageManager = new Sidvall.IO.StorageManager(new Sidvall.IO.FileSystem(Sidvall.IO.FolderType.LocalApplicationData)),

                // Client
                PrincipalContextManager = new Sidvall.Security.PrincipalContextManager(new Sidvall.Security.PrincipalContextProvider()),
                //PrincipalManager = new Sidvall.Security.PrincipalManager(new Sidvall.Security.PrincipalProvider()),

                // Host specific
                CultureManager = new Sidvall.Globalization.CultureManager(new Sidvall.Globalization.CultureProvider()),
                ResourceManager = new Sidvall.Resources.ResourceManager(new Sidvall.Resources.ResourceProvider()),
                SettingsManager = new Sidvall.Configuration.SettingsManager(new Sidvall.IdentityManager.Test.Configuration.ApplicationSettings()),
            };

            // Application specific
            Sidvall.IdentityManager.SystemContext.Current.InitializeServerDataLayer(Properties.Settings.Default.ConnectionString);
        }

        #endregion
    }
}
